﻿/**
 * @file MainForm
 *
 * @brief 
 *
 * @author gyzheng
 *
 * @mail gyazheng@gmail.com
 *
 * @date 07/27/2011 22:37:51 
 *
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CarFrameWork.Model;
using CarFrameWork.Common;
using CarFrameWork.UI.Common;

namespace CarFrameWork.UI.SystemManage.UserManage
{
    public partial class UserFormBase : DetailForm
    {
        protected UserInfo m_UserInfo;
        public UserFormBase()
        {
            InitializeComponent();     
        }

        private void quitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UserFormBase_Load(object sender, EventArgs e)
        {
            this.cmbGroup.DataSource = GetGroupList("").Tables[0].DefaultView;
            this.cmbGroup.DisplayMember = "GroupName";
            this.cmbGroup.ValueMember = "ID";

            this.cmbRole.DataSource = GetRoleList("").Tables[0].DefaultView;
            this.cmbRole.DisplayMember = "RoleName";
            this.cmbRole.ValueMember = "RoleID";
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetRoleList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [RoleInfo] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        private DataSet GetGroupList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [GroupInfo] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query("select * FROM [GroupInfo] ");
        }

        private bool InspectionText(TextBox text)
        {
            if (string.Empty == text.Text.Trim())
                return false;
            return true;
        }

        protected bool TestContent()
        {
            if (!InspectionText(this.txtLoginName))
                return false;
            if (!InspectionText(this.txtPassword))
                return false;
            if (!InspectionText(this.txtUserName))
                return false;
            if (!InspectionText(this.txtDescript))
                return false;
            return true;
        }

        protected void SetUserToForm()
        {
            txtuserId.Text = m_UserInfo.UserID.ToString();
            txtUserName.Text = m_UserInfo.UserName;
            txtLoginName.Text = m_UserInfo.LoginName;
            txtPassword.Text = m_UserInfo.PassWord;
            txtDescript.Text = m_UserInfo.Description;

            foreach (DataRowView i in this.cmbGroup.Items)
            {

                if (Convert.ToInt32(i.Row[0].ToString()) == m_UserInfo.GroupID)
                {
                    this.cmbGroup.SelectedItem = i;
                    break;
                }
            }

            foreach (DataRowView i in this.cmbRole.Items)
            {
                if (Convert.ToInt32(i.Row[0].ToString()) == m_UserInfo.RoleID)
                {
                    this.cmbRole.SelectedItem = i;
                    break;
                }
            }
        }

        protected void GetFormToUser()
        {
            //m_UserInfo.UserID = Convert.ToInt32(txtuserId.Text);
            m_UserInfo.UserName = txtUserName.Text;
            m_UserInfo.LoginName = txtLoginName.Text;
            m_UserInfo.PassWord = txtPassword.Text;
            m_UserInfo.Description = txtDescript.Text;
            m_UserInfo.GroupID = Convert.ToInt32(this.cmbGroup.SelectedValue.ToString());
            m_UserInfo.RoleID = Convert.ToInt32(this.cmbRole.SelectedValue.ToString());
        }
    }
}
